package com.hxd.otc.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;

import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;

/**
 * 类  名 :  FilterEmojiEditText
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/27 9:56
 * 描  述 :  ${TODO}
 */
public class FilterEmojiEditText extends android.support.v7.widget.AppCompatEditText {
    //输入表情前的光标位置
    private int cursorPos;
    //输入表情前EditText中的文本
    private String inputAfterText;
    //是否重置了EditText的内容
    private boolean resetText;

    private Context mContext;

    public FilterEmojiEditText(Context context) {
        super(context);
        this.mContext = context;
        initEditText();
    }

    public FilterEmojiEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initEditText();
    }

    public FilterEmojiEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initEditText();
    }

    // 初始化edittext 控件
    private void initEditText() {
        this.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {
                if (!resetText) {
                    inputAfterText = s.toString();
                }

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (containsEmoji(s.toString())) {
                    resetText = true;
                    setText(inputAfterText);
                    setSelection(inputAfterText.length());
                } else {
                    resetText = false;
                }
                LogUtils.w("onTextChanged--", "onTextChanged:--" + s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.w("onTextChanged--", "afterTextChanged:--" + s.toString());
            }


        });
    }


    /**
     * 检测是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        if (DataUtils.isEmpty(source)) {
            return false;
        }
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是Emoji
     *
     * @param codePoint 比较的单个字符
     * @return
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }
}
